<?php 
	error_reporting(E_WARNING);
	include_once '../includes/metodos.php';
	include_once '../includes/ini.php';
	checkUser();
	connectSql();
	include_once '../header.php';
?>
	<body>
	<div class="main">
		<a href="http://localhost/CPIF/painel.php">Voltar</a><br><br><br>
		<form action="index.php" method="post">
		<label>Agenda de </label>
		<SELECT name="podo">
			<?php 
			$sql = mysql_query("SELECT * FROM tb_funcionario");
			while ($row = mysql_fetch_array($sql)){
				echo '<OPTION value="'.$row['id'].'">'.$row['nome'].' '.$row['sobrenome'].'</OPTION>';
			}
			?>
		</SELECT>
		<input type="hidden" name="cmd" value="agenda">
		<label>Data:</label><input type="text" id="datepicker" name="data">
		<input type="submit" value="Buscar">
		</form>
		<hr>
			<?php 
			if(isset($_POST['cmd']) || $_POST['cmd']=='agenda'){
				//formulario para marcar agenda
				$c = 1;
				$query = mysql_query("SELECT * FROM tb_funcionario WHERE id=".$_POST['podo']);
				$row = mysql_fetch_array($query);
				$funcionario = $row['nome'].' '.$row['sobrenome'];
				if($row['id']==1){
					$hora = array(1=>'08:00',2=>'08:40', 3=>'09:20',4=>'10:00',5=>'10:40', 6=>'11:20',7=>'12:00', 8=>'12:40', 9=>'13:20', 10=>'14:00', 11=>'14:40'
								, 12=>'15:20', 13=>'16:00', 14=>'16:40', 15=>'17:20', 16=>'18:00');
					$cont = 17;
				}else{
					$hora = array(1=>'08:00',2=>'09:00', 3=>'10:00',4=>'11:00',5=>'12:00', 6=>'13:00',7=>'14:00', 8=>'15:00', 9=>'16:00', 10=>'17:00', 11=>'18:00');
					$cont = 11;
				}
				if(empty($_POST['data'])){
					$data=date("Y-m-d");
				}else{
					$data = $_POST['data'];
				}
				$show_data = explode("-", $data);
				echo '<strong><label>Data: '.$show_data[2].'/'.$show_data[1].'/'.$show_data[0].'</label><br/>';
				echo '<label>'.$funcionario.'</label><br/></strong>';
				//Criando os horarios
				echo '<table border="1px">';
				?>
				<thead>
					<tr>
						<td><b>Hora</b></td>
						<td><b>Cliente</b></td>
						<td><b>A&ccedil;&atilde;o</b></td>
					</tr>
				</thead>
				<?php 
				while ($c<$cont){
					echo '<tr><td width="50px"><strong>'.$hora[$c].'</strong></td>';
					$agenda = mysql_query("SELECT * FROM tb_agenda WHERE hora='".$hora[$c].":00' AND data='".$data."' AND id_funcionario=".$_POST['podo']." AND (status=1 OR status=4) ORDER BY hora LIMIT 1");
					$num = mysql_num_rows($agenda);
					if($num>0){
						$row = mysql_fetch_array($agenda);
						if($row['status']==1){
							echo '<td width="320px"><label>'.utf8_encode($row['nm_cliente']).' '.$row['telefone'].' / '.$row['celular'].'</label></td>';
							echo '<td><form action="edit.php" method="post">
							<select name="cmd">
								<option value="faltou">Faltou</option>
								<option value="desmarcar">Desmarcar</option>
								<option value="editar">Editar</option>
								<option value="fim">Conclu&iacute;do</option>
							</select>
							<input type="hidden" name="id" value="'.$row['id'].'"/>
							<input type="submit" value="Executar"/>
							</form></td></tr>';	
						}else{
							echo '<td width="320px"><label class="texto" style="color: blue;">'.utf8_encode($row['nm_cliente']).' '.$row['telefone'].' / '.$row['celular'].'</label></td></tr>';
						}
					}else{
						echo '<td style="color: red; width: 100px"> Vago</td>';
						echo '<td><form action="new.php" method="post">
									<input type="hidden" name="hora" value="'.$hora[$c].'"/>
									<input type="hidden" name="data" value="'.$data.'"/>
									<input type="hidden" name="podo" value="'.$_POST['podo'].'"/>
									<input type="submit" value="Marcar" />
								</form></td><td></td></tr>';
					}
					++$c;
				}
				echo '</table>';
			}
		?>
		</div>
	</body>
</html>